package com.bootcamp.servlet.register;

/**
 * @program: wrok-1101
 * @description: ${description}
 * @author: Keyu Li
 * @create: 2021-11-01 20:46
 **/

import com.bootcamp.servlet.util.UserInfoBeanHandler;
import com.bootcamp.servlet.bean.UserInfo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

@WebServlet(name = "RegisterServlet", value = "/registerv1")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("Please use POST method!");
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符集为utf-8
        request.setCharacterEncoding("utf-8");

        // 打印请求报文
        printRequestToFile(request);

        // 获取请求参数 map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将map映射为对象
        UserInfo userInfo = UserInfoBeanHandler.transferMapToBean(parameterMap);
        // 打印对象 并 包装到response
        System.out.println(userInfo);
        response.setContentType("text/html;charset=UTF-8");
//        response.setCharacterEncoding("iso-8859-1");
        response.getWriter().println(userInfo);
    }

    private void printRequestToFile(HttpServletRequest request) throws IOException {
        // 获取输出流，输出到项目根目录
        ServletContext servletContext = request.getServletContext();
        String realPath = servletContext.getRealPath("userInfoRequest.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(realPath));

        // 得到请求行
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String protocol = request.getProtocol();
        String requestLine = method + " " + uri + " " + protocol;
//        System.out.println(requestLine);
        bufferedWriter.write(requestLine);
        bufferedWriter.newLine();
        bufferedWriter.flush();

        // 得到请求头
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String value = request.getHeader(key);
//            System.out.println(key+" : "+value);
            bufferedWriter.write(key + " : " + value);
            bufferedWriter.newLine();
        }
        bufferedWriter.flush();

        // 得到请求体参数
        bufferedWriter.newLine();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = parameterNames.nextElement();
            String[] values = request.getParameterValues(key);
            if (values.length == 1) {
                bufferedWriter.write(key + " : " + values[0] + "\n");
                continue;
            }
            StringBuffer valueBuffer = new StringBuffer(key + " : {");
            for (String value : values) {
                valueBuffer.append("\"" + value + "\",");
            }
            valueBuffer.deleteCharAt(valueBuffer.length() - 1);
            valueBuffer.append("}\n");
            bufferedWriter.write(valueBuffer.toString());
        }
        bufferedWriter.flush();

        // 关闭资源
        bufferedWriter.close();
    }
}
